<%@ page language="java" import="java.util.*,java.net.*,java.sql.*,java.io.*,javax.sql.*,javax.naming.*,java.text.*" pageEncoding="UTF-8"%>
<%!    private String propFile = "/sn_sv_db.properties";

    public Properties getPropertiesFiles() {
        Properties prop = new Properties();
        try {
            //System.out.println("loading properties file：" + this.propFile + "");
            prop.load(getClass().getResourceAsStream(propFile));
            //System.out.println("loaded successly!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            //System.out.println("properties file can not be found!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop;
    }

    public Connection getConnection() {
        Connection conn = null;
        String driver = null;
        String url = null;
        String username = null;
        String password = null;
        String key = null;
        String value = null;
        try {
            Properties prop = getPropertiesFiles();
            Set<Object> set = prop.keySet();
            for (Iterator<Object> it = set.iterator(); it.hasNext();) {
                key = (String) it.next();
//	            value = prop.getProperty(key);
                if (key.equals("jdbc.sqlserver.driver")) {
                    driver = prop.getProperty(key);
                } else if (key.equals("jdbc.sqlserver.url")) {
                    url = prop.getProperty(key);
                } else if (key.equals("jdbc.sqlserver.username")) {
                    username = prop.getProperty(key);
                } else if (key.equals("jdbc.sqlserver.password")) {
                    password = prop.getProperty(key);
                }
            }
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url, username, password);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    //关闭数据库连接
    public void closeConn(Connection conn, PreparedStatement pst, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
        }
    }
%>